package com.cloudcc.mobile.manager;

import android.text.TextUtils;

import com.cloudcc.cloudframe.util.DateUtils;
import com.cloudcc.cloudframe.util.StringUtils;
import com.litesuits.android.log.Log;

import java.util.Date;

/**
 * url管理
 * 
 * @author wangzhiqiang
 * @date 2015-7-27 上午11:26:40
 * 
 */
public class UrlManager {

	public static final String SERVER = "http://ccws.cloudcc.com";
	public static final String gengxin = "http://www.cloudcc.com/android/1.html";

	//public static String APP_UR = "http://ccws.cloudcc.com"; //原
//	public static String APP_UR = "http://47.96.254.11/";
	public static String APP_UR = "http://bull.cloudcc.com/";
	public static String WEB_DISTRIBUTOR = APP_UR + "/distributor.action";

	// public static String MOCK_URL =
	// "http://yalitest.cloudcc.cn/mobilemockapi/";
	public static String MOCK_URL = "http://192.168.1.45:8080/beau/";

	public static String MOCK_URL1 = "http://192.168.1.43:8080/";

	/**
	 * 绑定url
	 */
	public static String WEB_BINDING = APP_UR + "/bindinglogin.action?binding=";

	private static UrlManager mManager;

	public UrlManager() {
	}

	public static UrlManager getManager() {
		if (mManager == null) {
			mManager = new UrlManager();
		}
		return mManager;
	}

	/**
	 * 获得 用户头像 的url
	 * 
	 * @param authorId
	 * @return
	 */
	public static String getLogoUrl(String authorId) {
		String topimage = getInterfaceUrl()
				+ "?serviceName=showChatterImage&type=user&id=" + authorId
				+ "&binding=" + RunTimeManager.getInstance().getServerBinding();
		return topimage;
	}

	/**
	 * 快速添加 快捷方式
	 * 
	 * @param srcUrl
	 * @return
	 */
	public static String getFastNewUrl(String srcUrl) {
		String url = srcUrl.substring(srcUrl.lastIndexOf("=") + 1,
				srcUrl.length());
		return getRootUrl()
				+ "/weiquery.action?obj="
				+ url
				+ "&m=newpage&rtnURL=%2Fwx_lookAtRecent.action%3Fm%3Dwx_lookAtRecent%26obj%3D"
				+ url;
	}

	/**
	 * 获得待审批连接
	 * 
	 * @return
	 */
	public static String getApproval() {
		return getRootUrl() + "/WeiXin_getmangeAllAppRequest.action";
	}

	/**
	 * 是否是返回的url
	 * 
	 * @param url
	 * @return
	 */
	public static boolean isBackUrl(String url) {
		boolean contains = StringUtils.contains(url, getRootUrl()
				+ "/wx_lookAtRecent.action?m=wx_lookAtRecent&obj=");
		boolean contains2 = StringUtils
				.contains(
						url,
						getRootUrl()
								+ "/wx_taskquery.action?objid=task&taskcondition=today&m=list&rtnURL=");
		boolean contains3 = StringUtils
				.contains(
						url,
						getRootUrl()
								+ "/wx_taskquery.action?objid=event&eventcondition=today&m=list&rtnURL=");
		return contains || contains2 || contains3;
	}

	/**
	 * 是否是返回主页
	 * 
	 * @param url
	 * @return
	 */
	public static boolean isHome(String url) {
		return StringUtils.contains(url, "/WeiXin_index.action")
				|| StringUtils.contains(url, "/WeiXinLeshi_index.action");
	}

	public String getWapHome() {
		return getRootUrl() + "/WeiXin_index.action";
	}

	/**
	 * 是否将要跳转登录页面
	 * 
	 * @param url
	 * @return
	 */
	public static boolean isWapLogin(String url) {
		Log.d("isWapLogin", url + "::" + getRootUrl() + "::"
				+ "/weixinlogin.action");
		return StringUtils.contains(url, "https://login.cloudcc.com/")|| StringUtils.contains(url, getRootUrl()
				+ "/weixinlogin.action");
	}
//	
	public static String getInterfaceUrl() {
		if (StringUtils.equals(APP_UR, "http://ccws.cloudcc.com")) {
			APP_UR = "";
		}
		return APP_UR + "/distributor.action";
	}
	//扫码接口传值
	public static String getInterfaceSao() {
		if (StringUtils.equals(APP_UR, "http://ccws.cloudcc.com")) {
			APP_UR = "";
		}
		return APP_UR;
	}
	
	public static String getInterfaceYm() {
		if (StringUtils.equals(APP_UR, "http://ccws.cloudcc.com")) {
			APP_UR = "";
		}
		return APP_UR;
	}

	public static String getRootUrl() {
		if (StringUtils.equals(APP_UR, "http://ccws.cloudcc.com")) {
			APP_UR = "";
		}
		APP_UR = convertRootUrl(APP_UR);
		return APP_UR;
	}

	public static String getBindingUrl() {
		return getRootUrl() + "/bindinglogin.action?binding=";
	}

	/**
	 * 新增任务
	 * 
	 * @deprecated
	 */
	public String getTaskNewUrl() {
		return getRootUrl()
				+ "/wx_taskquery.action?m=newpage&obj=bfa&rtnURL=%2Fwx_taskquery.action%3Fobjid%3Dtask%26taskcondition%3Dtoday%26m%3Dlist";
	}

	/**
	 * 新增日程事件
	 * 
	 * @deprecated
	 */
	public String getEventNewUrl() {
		return getRootUrl()
				+ "/wx_taskquery.action?m=newpage&obj=bef&rtnURL=%2Fwx_taskquery.action%3Fobjid%3Devent%26eventcondition%3Dtoday%26m%3Dlist";
	}

	/**
	 * 获得 新建记录的 url
	 * 
	 * @return
	 */
	public String getNewRecordUrl(String objId) {
		return getRootUrl() + "/weiquery.action?m=newpage&obj=" + objId;
	}

	/**
	 * 获得 记录详情的url'
	 * 
	 * @param id
	 * @return
	 */
	public String getEditRecordUrl(String id) {
		return getRootUrl() + "/weiquery.action?m=query&id=" + id;
	}

	/**
	 * 获取日程或者 任务的新建wap页面
	 * 
	 * @param isTask
	 *            是不是 任务 如果是任务 填写 true
	 * @return
	 */
	public String getNewTaskOrSchedule(boolean isTask) {
		return getRootUrl() + "/wx_taskquery.action?m=newpage&obj="
				+ (isTask ? "bfa" : "bef");
	}
	//详情页面新建任务
//	?relateid="+id+"&relateobj="+obj+"&recordId="+id+"&m=query&ismainpage=&rtnURL=%2FWeiXin_index.action";

	public static String getNewTaskOrSchedule_x(boolean isTask,String id,String objid) {
		return getRootUrl() + "/wx_taskquery.action?m=newpage&obj="
				+ (isTask ? "bfa" : "bef")+"&relateid="+id+"&relateobj="+objid+"&recordId="+id;
	}
	public static String getNewTaskOrSchedule_y(boolean isTask,String id,String objid) {
		return getRootUrl() + "/wx_taskquery.action?m=newpage&obj="
				+ (isTask ? "bfa" : "bef")+"&whoid="+id+"&whoobj="+objid+"&recordId="+id;
	}

	public String getNewTaskOrSchedule(boolean isTask, Date data) {
		String dateN = DateUtils.dateToString(data, "yyyy-MM-dd");
		String dateH = DateUtils.dateToString(data, "HH:mm:ss");
		Log.d("shuchudate", "时间是" + dateN + "::" + dateH);
		return getRootUrl() + "/wx_taskquery.action?m=newpage&obj="
				+ (isTask ? "bfa" : "bef") + "&starttime=" + dateN + "%20"
				+ dateH;
	}

	/**
	 * 获得 日程或者任务的详情
	 * 
	 * @param id
	 * @return
	 */
	public String getTaskOrScheduleDetailUrl(String id) {
		return getRootUrl() + "/wx_taskquery.action?m=query&id=" + id;
	}

	// 获得备注及附件
	public String getTaskOrScheduleDetailUrl1(String id) {
		return getRootUrl() + "/weidocument.action?m=detail&id=" + id;
	}

	/**
	 * 获得对象 详情 无标题 url
	 * 
	 * @param id
	 * @return
	 */
	public String getEntityDetailNoHeaderUrl(String id) {
		return getRootUrl() + "/weiquery.action?m=detail4Native&id=" + id;
	}

	/**
	 * 获得附件及详情对象 详情 无标题 url
	 * 
	 * @param id
	 * @return http://test.cloudcc.cn/weidocument.action?m=detail&id=
	 *         bda20165B0492C0QstVZ&noHeader=true
	 */
	public String getEntityDetailNoHeaderUrl1(String id) {
		return getRootUrl() + "/weidocument.action?m=detail&id=" + id
				+ "&noHeader=true";
	}

	/**
	 * 获取实体 详情 编辑url
	 * 
	 * @param id
	 * @return
	 */

//日程事件进入详情页
	public String getEntityDetailEditUrlTaskEvent(String id) {
		return getRootUrl() + "/wx_taskquery.action?m=edit&id=" + id;
	}
	public String getEntityDetailEditUrl(String id) {
		return getRootUrl() + "/weiquery.action?m=edit&id=" + id;
	}

	public String getEntityDetailEditUrl1(String id) {
		return getRootUrl() + "/weidocument.action?m=edit&id=" + id;
	}

	public String getUrlByRoot(String url) {
		return getRootUrl() + getHeaderUrl(url);
	}

	/*
	 * 获取重新分配url
	 */
	public String getReallocate(String workItemId, String relatedId) {
		return getRootUrl() + "/WeiXin_reassign.action?workItemId="
				+ workItemId + "&relatedId=" + relatedId
				+ "&retURL=/WeiXin_index.action";
	}

	/*
	 * 获取进行审批url
	 */
	public String getGoApproval(String workItemId, String relatedId) {
		return getRootUrl() + "/WeiXin_approval.action?workItemId="
				+ workItemId + "&relatedId=" + relatedId
				+ "&actionType=process&retURL=/WeiXin_index.action";
	}

	/**
	 * 帮助 url 增加返回
	 * 
	 * @param url
	 * @return
	 */
	public String addReturnUrl(String url) {
		return url + "&rtnURL=/WeiXin_index.action";
	}
	public String addReturnUrl_jl(String url) {
		return url +"&rtnURL=/WeiXin_index.action";
	}
	/**
	 * 帮助 url 增加返回
	 *
	 * @param url
	 * @return
	 */


	public String addReturnUrl_x(String url,String name,String shouji) {
		return url +"&rtnURL=/WeiXin_index.action"+"&name="+name+"&shouji="+shouji;
	}
	public String addReturnUrl_x_dianhua(String url,String name,String shouji) {
		return url +"&rtnURL=/WeiXin_index.action"+"&name="+name+"&qtdh="+shouji;
	}
	public String addReturnUrl_x_nohe(String url,String name,String shouji) {
		return url+"&isNotShowHeader=true"+"&rtnURL=/WeiXin_index.action"+"&name="+name+"&shouji="+shouji;
	}
	public String addReturnUrl_x_dianhua_nohe(String url,String name,String shouji) {
		return url+"&isNotShowHeader=true" +"&rtnURL=/WeiXin_index.action"+"&name="+name+"&qtdh="+shouji;
	}
	public String addReturnfirstUrl(String url) {
		return url;
	}

	/**
	 * 转换
	 * 
	 * @param url
	 * @return
	 */
	public static String convertRootUrl(String url) {
		if (TextUtils.isEmpty(url)) {
			return url;
		}
		if (StringUtils.endsWith(url, "/")) {
			return StringUtils.removeEnd(url, "/");
		}
		return url;
	}

	public static String getHeaderUrl(String url) {
		if (TextUtils.isEmpty(url)) {
			return url;
		}
		if (!StringUtils.startsWith(url, "/")) {
			return "/" + url;
		}
		return url;
	}

}
